﻿using SachOnlineLibrary.BLL;
using SachOnlineLibrary.Public;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebFormDatHang : System.Web.UI.Page
{
    DonDatHangBLL lib = new DonDatHangBLL();
    public static List<DonDatHang> listdondathang;
    protected void Page_Load(object sender, EventArgs e)
    {
        HienThiThongBao("");
        if (!IsPostBack)
        {
            gdvDatHang_Load();
        }
    }
    private void gdvDatHang_Load()
    {
        listdondathang = lib.LietKeTatCaDonDatHang();
        Session["listdondathang"] = listdondathang;
        gdvDatHang.DataSource = listdondathang;
        gdvDatHang.DataBind();
    }
    protected void gdvDatHang_RowEditing(object sender, GridViewEditEventArgs e)
    {
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
    public static string HienChiTietDatHang(int maphieu)
    {
        List<ChiTietDatHang> list = new DonDatHangBLL().LietKeChiTietDatHang(maphieu);

        string str = "<table cellspacing='15px' style='font-family: Tahoma; font-size: 13px;'>";
        str += "<tr><th>Tên sách</th><th>Số lượng</th><th>Đơn giá</th></tr>";

        foreach (ChiTietDatHang item in list)
        {
            str += "<tr>";
            str += string.Format("<td><img src='images/{0}' width='50px'/><br>{1}</td>", item.Sach.Hinh, item.Sach.TenSach);
            str += string.Format("<td style='vertical-align: bottom;'>{0}</td><td style='vertical-align: bottom;'>{1}</td>", item.SoLuong, item.DonGia);
            str += "</tr>";
        }

        str += "</table>";
        return str;
    }
    protected void gdvDatHang_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    private void HienThiThongBao(string thongbao)
    {
        lblThongBao.Text = thongbao;
    }
    protected void gdvDatHang_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Giaohang")
        {
            if (lib.GiaoHang(int.Parse(e.CommandArgument.ToString())))
            {
                gdvDatHang_Load();
                HienThiThongBao("Đơn hàng đã được cập nhật");
            }
            else
            {
                HienThiThongBao("Đơn hàng cập nhật thành công, lỗi server, vui lòng thử lại");
            }
        }
        if (e.CommandName == "Tralai")
        {
            if (lib.TraLaiHang(int.Parse(e.CommandArgument.ToString())))
            {
                gdvDatHang_Load();
                HienThiThongBao("Đơn hàng đã được cập nhật");
            }
            else
            {
                HienThiThongBao("Đơn hàng cập nhật thành công, lỗi server, vui lòng thử lại");
            }
        }
    }
    protected void gdvDatHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gdvDatHang.PageIndex = e.NewPageIndex;
        gdvDatHang_Load();
    }
    protected void btnTimKiem_Click(object sender, EventArgs e)
    {
        DateTime ngay = DateTime.Parse(txtDateTime.Text.ToString());
        Session["listdondathang"] = listdondathang.Where(d => d.NgayDatHang.Date.Equals(ngay.Date)).ToList();
        gdvDatHang.DataSource = Session["listdondathang"];
        gdvDatHang.DataBind();
    }
    protected void ddlHienThi_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlHienThi.SelectedIndex==0)
        {
            List<DonDatHang> list = (List<DonDatHang>)Session["listdondathang"];
            gdvDatHang.DataSource = listdondathang.Where(s => s.TrangThai.Equals("Chưa giao")).ToList();
            gdvDatHang.DataBind();
        }
        else
        {
            gdvDatHang.DataSource = (List<DonDatHang>)Session["listdondathang"];
            gdvDatHang.DataBind();
        }
    }
}